import { DataSet } from 'choerodon-ui/pro';
import Record from 'choerodon-ui/pro/lib/data-set/Record';

export const gatesOperatorData = [
  {
    meaning: '高于',
    value: 'GT',
  },
];

export const newGatesMetricData = [
  {
    meaning: '总问题数',
    value: 'new_violations',
  },
  {
    meaning: 'Blocker问题数',
    value: 'new_blocker_violations',
  },
  {
    meaning: 'Critical问题数',
    value: 'new_critical_violations',
  },
  {
    meaning: 'Major问题数',
    value: 'new_major_violations',
  },
  {
    meaning: 'Minor问题数',
    value: 'new_minor_violations',
  },
  {
    meaning: '代码重复率',
    value: 'new_duplicated_lines_density',
  },
];

export const checkGatesValue = (value:any, name:string, record:Record) => {
  if (['new_violations', 'violations'].includes(record?.get('gatesMetric'))) {
    if (!(/^[0-9]*[1-9][0-9]*$/).test(value)) {
      return '仅支持输入非负整数';
    }
    return true;
  }
  if (['new_duplicated_lines_density', 'duplicated_lines_density'].includes(record?.get('gatesMetric'))) {
    if (value < 0) {
      return '仅支持输入非负数';
    }
    if (value.toString().split('.')[1] && value.toString().split('.')[1].length > 1) {
      return '仅支持输入到小数点后1位';
    }
    return true;
  }
  return true;
};

export const getGatesValueLabel = ({ record }: {record:Record}) => {
  if (['new_duplicated_lines_density', 'duplicated_lines_density'].includes(record?.get('gatesMetric'))) {
    return '值(%)';
  }
  return '值';
};

export default () => ({
  autoQuery: false,
  autoCreate: false,
  fields: [
    {
      name: 'gatesMetric',
      label: '门禁项（新代码）',
      required: true,
      options: new DataSet({
        data: newGatesMetricData,
      }),
    },
    {
      name: 'gatesOperator',
      label: '比较逻辑',
      defaultValue: 'GT',
      options: new DataSet({
        data: gatesOperatorData,
      }),
    },
    {
      name: 'gatesValue',
      label: '值',
      type: 'number' as any,
      required: true,
      validator: checkGatesValue,
      dynamicProps: {
        label: getGatesValueLabel,
      },
    },
  ],
});
